-- 运营活动: 天降奇宝


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动  
    act_id = 55, -- 活动id (55 天降奇宝)
    act_name = "天降奇宝", -- 活动名称
    act_desc = "充值对应金额可以获取相对奖励", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1593267400,  -- 活动结束时间(unix timestamp)
    config = { -- 配置数据
        DefaultCount = 3,   --默认次数
        PreTime = 1800,     --多少秒获取一次免费次数
        CostMoney = 30,     --多少元宝可以不扣次数抽一次
        maxCount = 10,      --最多可以获取的免费次数
        itemList =  
        {
            {    --最差的
                { rewardId = 1, weight = 18, type = 0, id = 514, count = 1, bind = 1, },
                { rewardId = 2, weight = 30, type = 0, id = 513, count = 3, bind = 1, },
                { rewardId = 3, weight = 36, type = 0, id = 645, count = 25, bind = 1, },
                { rewardId = 4, weight = 36, type = 0, id = 534, count = 1, bind = 1, },
                { rewardId = 5, weight = 36, type = 0, id = 643, count = 5, bind = 1, },
                { rewardId = 6, weight = 72, type = 0, id = 538, count = 1, bind = 1, },
                { rewardId = 7, weight = 60, type = 0, id = 645, count = 15, bind = 1, },
                { rewardId = 8, weight = 36, type = 0, id = 529, count = 1, bind = 1, },
                { rewardId = 9, weight = 60, type = 0, id = 643, count = 3, bind = 1, },
                { rewardId = 10, weight = 60, type = 0, id = 523, count = 3, bind = 1, },
                { rewardId = 11, weight = 72, type = 0, id = 645, count = 5, bind = 1, },
                { rewardId = 12, weight = 40, type = 0, id = 763, count = 3, bind = 1, },
                { rewardId = 13, weight = 60, type = 0, id = 643, count = 1, bind = 1, },
                { rewardId = 14, weight = 36, type = 0, id = 508, count = 1, bind = 1, },
                { rewardId = 15, weight = 60, type = 0, id = 645, count = 10, bind = 1, },
                { rewardId = 16, weight = 60, type = 0, id = 528, count = 3, bind = 1, },
                { rewardId = 17, weight = 45, type = 0, id = 643, count = 2, bind = 1, },
                { rewardId = 18, weight = 36, type = 0, id = 524, count = 1, bind = 1, },
                { rewardId = 19, weight = 45, type = 0, id = 645, count = 20, bind = 1, },
                { rewardId = 20, weight = 72, type = 0, id = 533, count = 1, bind = 1, },
                { rewardId = 21, weight = 45, type = 0, id = 643, count = 4, bind = 1, },
                { rewardId = 22, weight = 36, type = 0, id = 539, count = 1, bind = 1, },
                { rewardId = 23, weight = 30, type = 0, id = 645, count = 30, bind = 1, },
                { rewardId = 24, weight = 45, type = 0, id = 517, count = 2, bind = 1, },
                { rewardId = 25, weight = 18, type = 0, id = 518, count = 1, bind = 1, },
            },
            {   
                { rewardId = 1, weight = 18, type = 0, id = 514, count = 1, bind = 1, },
                { rewardId = 2, weight = 30, type = 0, id = 513, count = 3, bind = 1, },
                { rewardId = 3, weight = 36, type = 0, id = 645, count = 25, bind = 1, },
                { rewardId = 4, weight = 36, type = 0, id = 534, count = 1, bind = 1, },
                { rewardId = 5, weight = 36, type = 0, id = 643, count = 5, bind = 1, },
                { rewardId = 6, weight = 72, type = 0, id = 538, count = 1, bind = 1, },
                { rewardId = 7, weight = 60, type = 0, id = 645, count = 15, bind = 1, },
                { rewardId = 8, weight = 36, type = 0, id = 529, count = 1, bind = 1, },
                { rewardId = 9, weight = 60, type = 0, id = 643, count = 3, bind = 1, },
                { rewardId = 10, weight = 60, type = 0, id = 523, count = 3, bind = 1, },
                { rewardId = 11, weight = 72, type = 0, id = 645, count = 5, bind = 1, },
                { rewardId = 12, weight = 40, type = 0, id = 763, count = 3, bind = 1, },
                { rewardId = 13, weight = 60, type = 0, id = 643, count = 1, bind = 1, },
                { rewardId = 14, weight = 36, type = 0, id = 508, count = 1, bind = 1, },
                { rewardId = 15, weight = 60, type = 0, id = 645, count = 10, bind = 1, },
                { rewardId = 16, weight = 60, type = 0, id = 528, count = 3, bind = 1, },
                { rewardId = 17, weight = 45, type = 0, id = 643, count = 2, bind = 1, },
                { rewardId = 18, weight = 36, type = 0, id = 524, count = 1, bind = 1, },
                { rewardId = 19, weight = 45, type = 0, id = 645, count = 20, bind = 1, },
                { rewardId = 20, weight = 72, type = 0, id = 533, count = 1, bind = 1, },
                { rewardId = 21, weight = 45, type = 0, id = 643, count = 4, bind = 1, },
                { rewardId = 22, weight = 36, type = 0, id = 539, count = 1, bind = 1, },
                { rewardId = 23, weight = 30, type = 0, id = 645, count = 30, bind = 1, },
                { rewardId = 24, weight = 45, type = 0, id = 517, count = 2, bind = 1, },
                { rewardId = 25, weight = 18, type = 0, id = 518, count = 1, bind = 1, },
            },
            {   
                { rewardId = 1, weight = 18, type = 0, id = 514, count = 1, bind = 1, },
                { rewardId = 2, weight = 30, type = 0, id = 513, count = 3, bind = 1, },
                { rewardId = 3, weight = 36, type = 0, id = 645, count = 25, bind = 1, },
                { rewardId = 4, weight = 36, type = 0, id = 534, count = 1, bind = 1, },
                { rewardId = 5, weight = 36, type = 0, id = 643, count = 5, bind = 1, },
                { rewardId = 6, weight = 72, type = 0, id = 538, count = 1, bind = 1, },
                { rewardId = 7, weight = 60, type = 0, id = 645, count = 15, bind = 1, },
                { rewardId = 8, weight = 36, type = 0, id = 529, count = 1, bind = 1, },
                { rewardId = 9, weight = 60, type = 0, id = 643, count = 3, bind = 1, },
                { rewardId = 10, weight = 60, type = 0, id = 523, count = 3, bind = 1, },
                { rewardId = 11, weight = 72, type = 0, id = 645, count = 5, bind = 1, },
                { rewardId = 12, weight = 40, type = 0, id = 763, count = 3, bind = 1, },
                { rewardId = 13, weight = 60, type = 0, id = 643, count = 1, bind = 1, },
                { rewardId = 14, weight = 36, type = 0, id = 508, count = 1, bind = 1, },
                { rewardId = 15, weight = 60, type = 0, id = 645, count = 10, bind = 1, },
                { rewardId = 16, weight = 60, type = 0, id = 528, count = 3, bind = 1, },
                { rewardId = 17, weight = 45, type = 0, id = 643, count = 2, bind = 1, },
                { rewardId = 18, weight = 36, type = 0, id = 524, count = 1, bind = 1, },
                { rewardId = 19, weight = 45, type = 0, id = 645, count = 20, bind = 1, },
                { rewardId = 20, weight = 72, type = 0, id = 533, count = 1, bind = 1, },
                { rewardId = 21, weight = 45, type = 0, id = 643, count = 4, bind = 1, },
                { rewardId = 22, weight = 36, type = 0, id = 539, count = 1, bind = 1, },
                { rewardId = 23, weight = 30, type = 0, id = 645, count = 30, bind = 1, },
                { rewardId = 24, weight = 45, type = 0, id = 517, count = 2, bind = 1, },
                { rewardId = 25, weight = 18, type = 0, id = 518, count = 1, bind = 1, },
            },
            {    --最好的
                { rewardId = 1, weight = 18, type = 0, id = 514, count = 1, bind = 1, },
                { rewardId = 2, weight = 30, type = 0, id = 513, count = 3, bind = 1, },
                { rewardId = 3, weight = 36, type = 0, id = 645, count = 25, bind = 1, },
                { rewardId = 4, weight = 36, type = 0, id = 534, count = 1, bind = 1, },
                { rewardId = 5, weight = 36, type = 0, id = 643, count = 5, bind = 1, },
                { rewardId = 6, weight = 72, type = 0, id = 538, count = 1, bind = 1, },
                { rewardId = 7, weight = 60, type = 0, id = 645, count = 15, bind = 1, },
                { rewardId = 8, weight = 36, type = 0, id = 529, count = 1, bind = 1, },
                { rewardId = 9, weight = 60, type = 0, id = 643, count = 3, bind = 1, },
                { rewardId = 10, weight = 60, type = 0, id = 523, count = 3, bind = 1, },
                { rewardId = 11, weight = 72, type = 0, id = 645, count = 5, bind = 1, },
                { rewardId = 12, weight = 40, type = 0, id = 763, count = 3, bind = 1, },
                { rewardId = 13, weight = 60, type = 0, id = 643, count = 1, bind = 1, },
                { rewardId = 14, weight = 36, type = 0, id = 508, count = 1, bind = 1, },
                { rewardId = 15, weight = 60, type = 0, id = 645, count = 10, bind = 1, },
                { rewardId = 16, weight = 60, type = 0, id = 528, count = 3, bind = 1, },
                { rewardId = 17, weight = 45, type = 0, id = 643, count = 2, bind = 1, },
                { rewardId = 18, weight = 36, type = 0, id = 524, count = 1, bind = 1, },
                { rewardId = 19, weight = 45, type = 0, id = 645, count = 20, bind = 1, },
                { rewardId = 20, weight = 72, type = 0, id = 533, count = 1, bind = 1, },
                { rewardId = 21, weight = 45, type = 0, id = 643, count = 4, bind = 1, },
                { rewardId = 22, weight = 36, type = 0, id = 539, count = 1, bind = 1, },
                { rewardId = 23, weight = 30, type = 0, id = 645, count = 30, bind = 1, },
                { rewardId = 24, weight = 45, type = 0, id = 517, count = 2, bind = 1, },
                { rewardId = 25, weight = 18, type = 0, id = 518, count = 1, bind = 1, },
            },
        },    
    },
}


]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_55_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.itemList == nil then return 23 end
    if type(config.itemList) ~= "table" then return 24 end
    
    for id, itemNewList in ipairs(config.itemList) do
        if itemNewList == nil then return 25 end
        if type(itemNewList) ~= "table" then return 26 end
        if not oper_act_check_one_award_cfg(itemNewList) then
            return 22
        end 
    end
    

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_55_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_55_unsetup(cfg)   
end

-- 活动开始
function oper_act_55_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_55_start = 1
    ssVar.oper_act_55_cmd = cfg.cmd_id
    ssVar.oper_act_55_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_55, 1)

    local needClearState = true

    local crossDayState = false

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            needClearState = false
            local time = System.getSystemTime()
            if myData and myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
                crossDayState = true
            end
        end
    end

    if needClearState or crossDayState then
        gRankMgr_Clear(RANK_DEFINE_OPERATION_55)
    end

    local players = LuaHelp.getAllActorList()
    if players then 
        for k, v in ipairs(players) do  
            oper_act_55_login(sysarg, false)
        end
    end

end

-- 活动结束
function oper_act_55_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_55_start = nil
    ssVar.oper_act_55_cmd = nil
    ssVar.oper_act_55_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end
end

-- 打包数据
function oper_act_55_packet_data(sysarg, cfg)
    if not cfg then return end
    local config = cfg.config
    if not config then return end
    
    
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_55, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end
    local needSave = false
    if not myData.myChoiceList or oper_act_55_all_get(myData, config) then
        needSave = true
        myData.myChoiceList, myData.myChoiceSpecialState = oper_act_55_refresh_my_reward_list(config)
        myData.myReawrdState = {}
    end

    if not myData.myReawrdState then
        myData.myReawrdState = {}
    end

    if not myData.myChoiceSpecialState then
        myData.myChoiceSpecialState = {}
    end

    local miniTime = System.getCurrMiniTime()
    local time = System.getSystemTime()

    if not myData.dailyStartTime then
        myData.dailyStartTime = miniTime
    end
    if not myData.onLineTime then
        myData.onLineTime = 0
    end
    if not myData.myDailyUseCount then
        myData.myDailyUseCount = 0
    end
    local totalCount = math.floor((miniTime - myData.dailyStartTime + myData.onLineTime)/config.PreTime) + config.DefaultCount

    local moreTime = (miniTime - myData.dailyStartTime + myData.onLineTime) % config.PreTime
    local needTime = config.PreTime - moreTime
    if totalCount >= config.maxCount then
        totalCount = config.maxCount
        needTime = -1
    end
    local lastCount = totalCount - myData.myDailyUseCount

    if (myData.dailyStartTime and myData.dailyStartTime == miniTime) or needSave then
        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        myData.mySelfTime = time

        local newData = BaseFuc_serialize(myData)
        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)
    end
    
    local npack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (npack == nil) then
        return
    end
    --print("lastCount ="..lastCount)

    DataPack.writeByte(npack, lastCount)
    --print("needTime ="..needTime)
    DataPack.writeInt(npack, needTime)
    local len = #config.itemList
    DataPack.writeByte(npack, len)
    for idx = 1, len do
        if not myData.myChoiceList[idx] then
            myData.myChoiceList[idx] = {}
        end
        if not myData.myReawrdState[idx] then
            myData.myReawrdState[idx] = {}
        end
        if not myData.myChoiceSpecialState[idx] then
            myData.myChoiceSpecialState[idx] = {}
        end
        DataPack.writeByte(npack, config.everyRowNum)
        for ii = 1, config.everyRowNum do
            DataPack.writeByte(npack, myData.myChoiceList[idx][ii] or 0)
            DataPack.writeByte(npack, myData.myReawrdState[idx][ii] or 0)
            DataPack.writeByte(npack, myData.myChoiceSpecialState[idx][ii] or 0)
        end
    end

    DataPack.flush(npack) 
end

-- 操作，返回操作结果，0为成功
function oper_act_55_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_55_do cfg.config confg error")
        return
    end

    local opType = DataPack.readByte(clientPkt)

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_55, 1)

    local myData = BaseFuc_unserialize(data)

    if opType == 1 then

        if not myData then
            myData = {}
        end

        local miniTime = System.getCurrMiniTime()
        local time = System.getSystemTime()

        if not myData.dailyStartTime then
            myData.dailyStartTime = miniTime
        end
        if not myData.onLineTime then
            myData.onLineTime = 0
        end
        if not myData.myDailyUseCount then
            myData.myDailyUseCount = 0
        end

        local needResendData = false
        if not myData.myChoiceList or oper_act_55_all_get(myData, config) then
            myData.myChoiceList, myData.myChoiceSpecialState = oper_act_55_refresh_my_reward_list(config)
            myData.myReawrdState = {}
            needResendData = true
        end

        if not myData.myReawrdState then
            myData.myReawrdState = {}
        end

        if not myData.myChoiceSpecialState then
            myData.myChoiceSpecialState = {}
        end

        local totalCount = math.floor((miniTime - myData.dailyStartTime + myData.onLineTime)/config.PreTime) + config.DefaultCount

        local moreTime = (miniTime - myData.dailyStartTime + myData.onLineTime) % config.PreTime
        local needTime = config.PreTime - moreTime
        if totalCount >= config.maxCount then
            totalCount = config.maxCount
            needTime = -1
        end
        local lastCount = totalCount - myData.myDailyUseCount

        local needCostMoney = false
        if myData.myDailyUseCount >= config.maxCount then
           needCostMoney = true
        end

        if lastCount <= 0 then
            needCostMoney = true
        end

        if needCostMoney then
            if Actor.getMoneyCount(sysarg, mtYuanbao) < config.CostMoney then
                Actor.sendItemLack( sysarg, qatYuanbao, 0, config.CostMoney )
                return
            end

            if not Actor.delMoney( sysarg,mtYuanbao,config.CostMoney,LogIds.OperationLogId055,Lang.LogTips.Operation055) then
                return
            end
        else
            myData.myDailyUseCount = myData.myDailyUseCount + 1 
            lastCount = totalCount - myData.myDailyUseCount   
        end

        local getAwardList, getIndexList = oper_act_55_get_award_list(myData, config)

        if getAwardList == {} then
            return
        end

        local index, item = GetItemIdxRandExt(getAwardList)

        local myIndexValue = getIndexList[index]
        local myidx = math.floor(myIndexValue/10)
        local myii = myIndexValue%10
        if not myData.myReawrdState[myidx]  then
            myData.myReawrdState[myidx] = {}
        end
        if not myData.myChoiceSpecialState[myidx] then
            myData.myChoiceSpecialState[myidx] = {}
        end
        local myItemSpecialState = myData.myChoiceSpecialState[myidx][myii] or 0
        myData.myReawrdState[myidx][myii] = 1

        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        myData.mySelfTime = time

        local newData = BaseFuc_serialize(myData)
        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)

        if IsBagGridEnough(sysarg, {item}) then
            Awards.Give(sysarg,{item},LogIds.OperationLogId055,Lang.LogTips.Operation055)
        else
            local actorId = Actor.getActorId(sysarg)
            local title = Lang.ScriptTips.Operation139
            local content = Lang.ScriptTips.Operation140

            local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

            Awards.GiveToMailById(actorId, {item}, title, content, LogIds.OperationLogId055, Lang.LogTips.Operation055, vocation, sex)
        end 


        local netPack = oper_act_alloc_do_result_packet(sysarg, cfg.cmd_id, cfg.act_id, 0)
        if not netPack then return end
        DataPack.writeByte(netPack, lastCount)
        DataPack.writeInt(netPack, needTime)
        DataPack.writeByte(netPack, myidx)
        DataPack.writeByte(netPack, myii)
        DataPack.writeByte(netPack, item.rewardId)
        DataPack.writeByte(netPack, 1)
        DataPack.writeByte(netPack, myItemSpecialState)
        DataPack.flush(netPack)

        if oper_act_55_all_get(myData, config) then
            myData.myChoiceList, myData.myChoiceSpecialState = oper_act_55_refresh_my_reward_list(config)
            myData.myReawrdState = {}

            if not myData.cmd_id then
                myData.cmd_id = cfg.cmd_id
                myData.act_id = cfg.act_id
                myData.time = time
            end

            myData.mySelfTime = time

            local newData = BaseFuc_serialize(myData)
            gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)


            oper_act_55_packet_data(sysarg, cfg)

            return
        end

        if needResendData then
            oper_act_55_packet_data(sysarg, cfg)
        end

    elseif opType == 2 then
        if not config.RefreshCostMoney or config.RefreshCostMoney <= 0 then
            return
        end

        local lastCanGetNum = oper_act_55_last_count(myData, config)

        if lastCanGetNum == 0 then return end

        local costMoney = lastCanGetNum * config.RefreshCostMoney

        if Actor.getMoneyCount(sysarg, mtYuanbao) < costMoney then
            Actor.sendItemLack( sysarg, qatYuanbao, 0, costMoney )
            return
        end

        if not Actor.delMoney( sysarg,mtYuanbao,costMoney,LogIds.OperationLogId055,Lang.LogTips.Operation055) then
            return
        end
        if not myData then
            myData = {}
        end

        local miniTime = System.getCurrMiniTime()
        local time = System.getSystemTime()

        if not myData.dailyStartTime then
            myData.dailyStartTime = miniTime
        end
        if not myData.onLineTime then
            myData.onLineTime = 0
        end
        if not myData.myDailyUseCount then
            myData.myDailyUseCount = 0
        end

        myData.myChoiceList, myData.myChoiceSpecialState = oper_act_55_refresh_my_reward_list(config)
        myData.myReawrdState = {}

        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        myData.mySelfTime = time

        local newData = BaseFuc_serialize(myData)
        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)


        oper_act_55_packet_data(sysarg, cfg)
    end 
end


-- 跨天
function oper_act_55_new_day_arrive(act_id) 
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_55_start then
        return
    end
    if not ssVar.oper_act_55_cmd then
        return
    end
    if not ssVar.oper_act_55_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_55_cmd, ssVar.oper_act_55_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_55)

    local players = LuaHelp.getAllActorList()
    if players then 
        for k, v in ipairs(players) do  
            oper_act_55_login(sysarg, true)
        end
    end
end

--登入
function oper_act_55_login(sysarg, nextDayState)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_55_start then
        return
    end
    if not ssVar.oper_act_55_cmd then
        return
    end
    if not ssVar.oper_act_55_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_55_cmd, ssVar.oper_act_55_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_55, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myChoiceList or oper_act_55_all_get(myData, config) then
        needSave = true
        myData.myChoiceList, myData.myChoiceSpecialState = oper_act_55_refresh_my_reward_list(config)
        myData.myReawrdState = {}
    end

    local miniTime = System.getCurrMiniTime()
    local time = System.getSystemTime()

    
    myData.dailyStartTime = miniTime

    if not myData.onLineTime then
        myData.onLineTime = 0
    end
    if not myData.myDailyUseCount then
        myData.myDailyUseCount = 0
    end

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    myData.mySelfTime = time

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)

    if nextDayState then
        oper_act_55_packet_data(sysarg, cfg)
    end
end

--登出
function oper_act_55_logout(sysarg)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_55_start then
        return
    end
    if not ssVar.oper_act_55_cmd then
        return
    end
    if not ssVar.oper_act_55_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_55_cmd, ssVar.oper_act_55_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_55, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    local miniTime = System.getCurrMiniTime()
    local time = System.getSystemTime()

    if not myData.dailyStartTime then
        myData.dailyStartTime = miniTime
    end
    if not myData.onLineTime then
        myData.onLineTime = 0
    end
    if not myData.myDailyUseCount then
        myData.myDailyUseCount = 0
    end

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    myData.onLineTime = myData.onLineTime + miniTime - myData.dailyStartTime

    myData.dailyStartTime = miniTime

    myData.mySelfTime = time

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_55, 1, newData)
end

function oper_act_55_all_get(myData, config)
    if not config then
        return false
    end
    if not myData then
        myData = {}
    end
    if not myData.myReawrdState then
        myData.myReawrdState = {}
    end
    for idx = 1, #config.itemList do
        if not myData.myReawrdState[idx] then
            myData.myReawrdState[idx] = {}
        end
        for ii = 1, config.everyRowNum do
            local itemState = myData.myReawrdState[idx][ii] or 0
            if itemState == 0 then
                return false
            end
        end
    end
    return true
end


function oper_act_55_refresh_my_reward_list(config)
    local myTable = {}
    local mySpecial = {}
    for idx = 1, #config.itemList do
        myTable[idx] = {}
        mySpecial[idx] = {}
        local maxSpecialNum = 0
        if config.choiceSpecialNum and config.choiceSpecialNum[idx] then
            maxSpecialNum = config.choiceSpecialNum[idx]
        end
        local specialNum = System.getRandomNumber(maxSpecialNum) + 1
        local specialList = GetItemIdxsRand(config.itemList[idx], specialNum) 
        local myLst = GetItemIdxsRand(config.itemList[idx], config.everyRowNum - specialNum) 
        local specialIndex = 1
        local normalIndex = 1
        for ii = 1, config.everyRowNum do
            if specialIndex > specialNum then
                if myLst[normalIndex] then
                    myTable[idx][ii] = myLst[normalIndex].rewardId
                else
                    myTable[idx][ii] = 0
                end
                mySpecial[idx][ii] = 0
                normalIndex = normalIndex + 1
            elseif normalIndex > specialNum then
                if specialList[specialIndex] then
                    myTable[idx][ii] = specialList[specialIndex].rewardId
                else
                    myTable[idx][ii] = 0
                end
                mySpecial[idx][ii] = 1
                specialIndex = specialIndex + 1
            else
                local curRan = System.getRandomNumber(config.everyRowNum) + 1
                if curRan <= specialNum then
                    if specialList[specialIndex] then
                        myTable[idx][ii] = specialList[specialIndex].rewardId
                    else
                        myTable[idx][ii] = 0
                    end
                    mySpecial[idx][ii] = 1
                    specialIndex = specialIndex + 1
                else
                    if myLst[normalIndex] then
                        myTable[idx][ii] = myLst[normalIndex].rewardId
                    else
                        myTable[idx][ii] = 0
                    end
                    mySpecial[idx][ii] = 0
                    normalIndex = normalIndex + 1
                end
            end
            
        end
    end
    return myTable, mySpecial
end

function oper_act_55_last_count(myData, config)
    local number = 24
    if not config then
        return number
    end
    if not myData then
        myData = {}
    end
    if not myData.myReawrdState then
        myData.myReawrdState = {}
    end
    for idx = 1, #config.itemList do
        if not myData.myReawrdState[idx] then
            myData.myReawrdState[idx] = {}
        end
        for ii = 1, config.everyRowNum do
            local itemState = myData.myReawrdState[idx][ii] or 0
            if itemState == 1 then
                number = number - 1
            end
        end
    end
    return number
end

function oper_act_55_get_award_list(myData, config)
    local rewardList = {}
    local indexList = {}
    if not config then
        return rewardList,indexList
    end
    if not myData then
        myData = {}
    end
    if not myData.myReawrdState then
        myData.myReawrdState = {}
    end

    if not myData.myChoiceList then
       myData.myChoiceList = {} 
    end

    if not myData.myChoiceSpecialState then
        myData.myChoiceSpecialState = {}
    end

    for ii = 1, config.everyRowNum do
        for idx = 1, #config.itemList do
            if not myData.myReawrdState[idx] then
                myData.myReawrdState[idx] = {}
            end
            if not myData.myChoiceSpecialState[idx] then
                myData.myChoiceSpecialState[idx] = {}
            end
            if not myData.myChoiceList[idx] then
               myData.myChoiceList[idx] = {} 
            end
            local itemState = myData.myReawrdState[idx][ii] or 0
            if itemState == 0 then
                if myData.myChoiceList[idx][ii] and myData.myChoiceList[idx][ii] > 0 then
                    if myData.myChoiceSpecialState[idx][ii] and myData.myChoiceSpecialState[idx][ii] > 0 then
                        table.insert(rewardList, config.itemSpeicialList[idx][myData.myChoiceList[idx][ii]])
                    else
                        table.insert(rewardList, config.itemList[idx][myData.myChoiceList[idx][ii]])
                    end 
                    table.insert(indexList, idx*10 + ii)
                end
                break
            end
        end
    end
    return rewardList, indexList
end
